package com.bishe.bishe.Controller;

import com.bishe.bishe.Service.TypesService;
import com.bishe.bishe.pojo.Types;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/type")
@RestController
public class TypesController {
    @Autowired
    private TypesService typesService;
    @RequestMapping("/findall")
    public List<Types> findall() {
        return typesService.findall();
    }

    @RequestMapping("/findPage")/*分页+模糊查询，和上一个有区别，这个需要map数据*/
    public PageInfo<Types> findPage(@RequestBody Map searchMap, int page, int size) {
        return typesService.findPage(searchMap, page, size);
    }

   @RequestMapping("/insert")
    public int insert(@Validated@RequestParam(value="typeName", required = false) String typeName) {
        return typesService.insert(typeName);
    }

    @RequestMapping("/delete")
    public int delete(@Validated@RequestParam(value = "typeId", required = false)int typeId) {
        int typeState=0;
        return typesService.delete(typeId,typeState);
    }

    @RequestMapping("/update")
    @ResponseBody
    public int update(@RequestBody Types types) {
        return typesService.update(types);
    }

    @RequestMapping("/findTypes")
    public Types findTypes(@Validated@RequestParam(value = "typeId",required = false)int typeId) {
        return typesService.findTypes(typeId);
    }

    @RequestMapping("/findName")
    public Types findTypesByName(@Validated@RequestParam(value = "typeName",required = false)String typeName) {
        return typesService.findTypesByName(typeName);
    }

    @RequestMapping("/batch")
    public int batchupdate(@Validated @RequestBody List<Integer> idlist){
        int typeState=0;
        for (int i = 0; i < idlist.size(); i++) {
            typesService.delete(idlist.get(i),typeState);
        }
        return 1;
    }

}
